<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card -->
    <el-card>
      <!--  -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>
       <!-- 列表 -->
    <el-table :data="goodsList" style="width: 100%" border stripe>
         <el-table-column type="index"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="700x"></el-table-column>
      <!-- 商品价格 -->
      <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
      <!-- 重量 -->
      <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
      <!-- 创建日期 -->
      <el-table-column prop="add_time " label="创建日期">
          <template slot-scope="scope">
              {{scope.row.add_time | format('yyyy-MM-dd')}}
          </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column  label="操作">
          <template slot-scope="scope">
              <!-- 编辑 -->
              <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
              <!-- 删除 -->
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="delGood(scope.row.goods_id)"></el-button>
          </template>
      </el-table-column>
    </el-table>
    <!-- 分页操作 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
   
  </div>
</template>
<script>
// k*

export default {
    
  data() {
    return {
      // 添加商品名称
      addName: '',
      // 获取商品列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
    //   总页数
    total:0,
      goodsList: []
    }
  },
  methods: {
    //   获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
    console.log(res)
    
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.total)
    },
    // 根据分页获取列表
    handleSizeChange(newsize){
        console.log(newsize);
        this.queryInfo.pagesize = newsize
        this.getGoodsList()
        
    },
    handleCurrentChange(newpage){
        console.log(newpage);
        this.queryInfo.pagenum = newpage
        this.getGoodsList()
    },
    // 删除商品分类
    async delGood(id) {
        const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err =>err)
        // console.log(confirm);
        if(confirm == 'cancel') return this.$message.info('取消删除')
        const {data:res} = await this.$http.delete('/goods/' + id) 
        if(res.meta.status == !200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getGoodsList()
        
    },
    // 添加商品
    addGood(){
        this.$router.push('/goods/add')
    }

  },
  created() {
    this.getGoodsList()
  }
}
</script>
<style lang='css' scoped>
</style>